﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace QuanLyThuVien.DAO
{
    class GhiNhanMatSachDAO
    {
        public static void Them(PhieuGhiNhanMatSach S)
        {
            NorthwindDataContext context = new NorthwindDataContext();
            PhieuGhiNhanMatSach a = new PhieuGhiNhanMatSach();
            a.NgayGhiNHan = S.NgayGhiNHan;
            a.MaNhanVien = S.MaNhanVien;
            a.MaChiTietPhieuMuon = S.MaChiTietPhieuMuon;
            a.TienPhat = S.TienPhat;
            context.PhieuGhiNhanMatSaches.InsertOnSubmit(a);
            context.SubmitChanges();
        }

        public static DataGridView GetALLGNMS()
        {
            NorthwindDataContext db = new NorthwindDataContext();
            DataGridView dt = new DataGridView();
            var sql = from GNMS in db.PhieuGhiNhanMatSaches
                      join CTPM in db.ChiTietPhieuMuons on GNMS.MaChiTietPhieuMuon equals CTPM.MaChiTietPhieuMuon
                      join S in db.Saches on CTPM.MaSach equals S.MaSach
                      join PM in db.PhieuMuons on CTPM.MaPhieuMuon equals PM.MaPhieuMuon
                      join DG in db.DocGias on PM.MaDocGia equals DG.MaDocGia
                      join NV in db.NhanViens on GNMS.MaNhanVien equals NV.MaNhanVien
                      select new 
                      { 
                          GNMS.MaPhieuGhiNhanMatSach,
                          S.TenSach,
                          S.TriGia,
                          DG.HoTenDocGia,
                          GNMS.NgayGhiNHan,
                          NV.HoTenNhanVien,
                          S.MaSach,
                          GNMS.TienPhat,
                      };
            dt.DataSource = sql;
            return dt;
        }

        public static void Xoa(PhieuGhiNhanMatSach S)
        {
            NorthwindDataContext context = new NorthwindDataContext();
            PhieuGhiNhanMatSach a = context.PhieuGhiNhanMatSaches.Single(sGNMS => sGNMS.MaPhieuGhiNhanMatSach == S.MaPhieuGhiNhanMatSach);
            context.PhieuGhiNhanMatSaches.DeleteOnSubmit(a);
            context.SubmitChanges();

        }

    }
}
